<?php

namespace app\api\controller\v1;
use app\common\controller\Api;
use alyun\AliyunSms;
use app\common\model\PhoneCode;
use think\Cookie;
use think\Cache;
use ChuanglanSmsHelper\ChuanglanSmsApi;
use think\Session;
use app\common\model\Member;

class Tool extends Api
{
    
    protected $authenticate=['only'=>'index'];
    
    protected $AccessKeyID = null;
    protected $AccessKeySecret = null;
    protected $region = null;

    public function _initialize()
    {

        $ossConfig = config('thirdaccount.aliyun');
        $this->AccessKeyID = $ossConfig['accessKeyId'];
        $this->AccessKeySecret = $ossConfig['accessKeySecret'];
        $this->region = $ossConfig['endpoint'];

        parent::_initialize(); // TODO: Change the autogenerated stub

    }
    
    /**
     * @desc 发送短信
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sendSms(){
        $rule = [
            'phone|手机号码'  => 'require',
            'type|短信类别'  => 'require',
        ];
        $params = paramForm();//转化你参数
        $this->validate($params,$rule);//表单校验
        $phone = $params['phone'];
        $type = $params['type'];
        
        //校验是不是频繁发短信
        $isFilter = ['phone_num'=>$phone,'type'=>$type];
        $isPhoneCode = PhoneCode::where($isFilter)->order('create_time desc')->find();//获取同类别和手机号 相同的最近一个

//        if(isset($isPhoneCode['id'])){
//
//            if(time()-$isPhoneCode->getData('create_time')<5*60){
//                $rt = ['msg'=>'短信过于频繁','status'=>false];//发送失败
//                api(0,'短信发送过于频繁',$rt);
//            }
//        }
        if(!isMobile($phone)){
            error('短信发送失败,手机号码格式不对');
        };
        switch ($type){
            case 'test_sms':
            break;
            case 'bingphone':
            case 'register':
                $isUser = Member::get(['phone'=>$phone]);
                if(isset($isUser)){
                    error('该用户已注册');
                }
            break;
            case 'login':
            case 'chagne_pwd':
                $isUser = Member::get(['phone'=>$phone]);
                if(!isset($isUser)){
                    error('该用户未注册');
                }
            break;
            default:
                error('短信类别错误');
            break;
        }
        
        $code = randString(6);
        $cl = new ChuanglanSmsApi();
        $rt = $cl->sendSMS($phone,"【岛里巴巴】尊敬的用户，您的验证码是".$code."请妥善保管.");
        
        if($cl->execResult($rt)){
            
            //记录验证码
            $phone_code = new PhoneCode();
            $phone_code->code_status = 0;//初始化为未使用
            $phone_code->over_time = time()+5*60;
            $phone_code->BizId = $phone.time();
            $phone_code->phone_num = $phone;
            $phone_code->code = $code;
            $phone_code->type = $type;
            $phone_code->save();
            
            Session::set('smsid',$phone_code->BizId);

            if($phone_code->id){
                api(1,'短信验证码发送成功',null);
            }else{
                api(0,'短信验证码发送成功,保存记录失败',null);
            }
        }else{
            api(0,'短信验证码发送失败',null);
        }
    }


    /**
     * @param $url
     * @desc  获取微信签名
     *
     */
    public function getJsSign($url,$debug=false){

        $rule = [
            'url|需要签名的url'  => 'require',
        ];

        $params = paramForm();//转化你参数
        $this->validate($params,$rule);//表单校验

        // 创建SDK实例
//        $script = &  load_wechat('Script');
        $script = new \Wechat\WechatScript(config('thirdaccount.wechat_pub'));

        $pubConfig =  config('thirdaccount.wechat_pub');
        $appid =$pubConfig['appid'];
//        var_dump($appid);die;
        // 获取JsApi使用签名，通常这里只需要传 $ur l参数
        $options = $script->getJsSign($url,$debug);

        // 处理执行结果
        if($options===FALSE){
            // 接口失败的处理
            error($script->errMsg);
        }else{
            // 接口成功的处理
            api(1,'获取jssdk签名成功',$options);
        }

    }
    






}



